package com.easy.huiju.controller;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.easy.huiju.dto.CameraDto;
import com.easy.huiju.model.Mulu;
import com.easy.huiju.model.MuluRoot;
import com.easy.huiju.thread.MediaTransferFlvByFFmpeg;
import com.easy.huiju.thread.MediaTransferFlvByJavacv;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;

import java.util.*;
import java.util.concurrent.*;
@Slf4j
public class TestUtil {
    private static ExecutorService executor = ThreadUtil.newExecutor(5,10);


    public static void main(String[] args) throws FrameGrabber.Exception {
      String token = login();
//        1504437132431855616
       String ppid = createMl(token,"370904","1515646682044436480","1515646682090573824","泰安平台");
       List<MuluRoot> groupIds = getMl(token,"dc4db571156f78086d8861a4b2d74309","1515646682044436480",ppid);

    }
    public static int tableSize =10;
    public static String login() {
        String url = "http://172.20.208.40:20080/vp/user/login";
        Map map = new HashMap();
        map.put("userName","admin");
        map.put("password","Tsit@123");
        String ruslt = HttpUtil.post(url,JSONUtil.toJsonStr(map));
        JSON json = JSONUtil.parse(ruslt);
        System.out.println(json.getByPath("data"));
        return  json.getByPath("data").toString();
    }
    public  static String getDw(String token) throws FrameGrabber.Exception {
        String url = "http://172.20.208.40:20080/vp/cg/query_child_items";
        Map map = new HashMap();
        map.put("contain",true);
        map.put("id","1479285149827993600");
        map.put("name","");
        map.put("page",1);
        map.put("pageSize",2000);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        log.info(ruslt);
        return  null;
    };
    public static void tongbu(String token,String cgId,String proId,String sourceId) {
        String url = "http://172.20.208.40:20080/vp/cg/add_all_channel_items";
        Map map = new HashMap();
        map.put("cgid",cgId);
        map.put("id",proId);
        map.put("sourceId",sourceId);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        System.out.println(ruslt);
    }
    public static MuluRoot getAllMl(String token,String parentId,String cgId,String id) {
        //获取基础目录结构
        String url = "http://172.20.208.40:20080/vp/treev3/list";
        Map map = new HashMap();
        map.put("parentId",parentId);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        JSON json = JSONUtil.parse(ruslt);
        List<MuluRoot> rootList = JSONUtil.toList(json.getByPath("data").toString(),MuluRoot.class);
        for(MuluRoot root :rootList){

        }
return null;
    }
    public static List getMl(String token,String parentId,String cgId,String id) {
        //获取基础目录结构
        String url = "http://172.20.208.40:20080/vp/treev3/list";
        Map map = new HashMap();
        map.put("parentId",parentId);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        JSON json = JSONUtil.parse(ruslt);
        List<MuluRoot> rootList = JSONUtil.toList(json.getByPath("data").toString(),MuluRoot.class);
        List<String>dianwei = new ArrayList<>();
        for (MuluRoot muluRoot :rootList){
            if(muluRoot.getChildOnlineNum()>0 && muluRoot.getType()==0){
            String ppid = createMl(token,muluRoot.getDeviceid(),cgId,id,muluRoot.getName());
                getMl(token,muluRoot.getId(),cgId,ppid);
            }else{
                dianwei.add(muluRoot.getId());

            }
        }

      createDw(token,cgId,id,dianwei);
        return  null;
    }

//    public  static String getBofang(String dwId,String token) throws FrameGrabber.Exception {
//        String url = " http://172.20.208.40:20080/vp/stream/play_stream_info/"+dwId;
//        String ruslt= HttpRequest.get(url).header("token",token)
//                .execute().body();
//        JSON json = JSONUtil.parse(ruslt);
//        System.out.println(json);
//        String rtsp = JSONUtil.parse(json.getByPath("data")).getByPath("rtmp").toString();
//        CameraDto cameraDto = new CameraDto();
//        cameraDto.setUrl(rtsp);
//        cameraDto.setId(dwId);
//        cameraDto.setName(JSONUtil.parse(json.getByPath("data")).getByPath("name").toString());
//        // 拉流器
//        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(cameraDto.getUrl());
//        // 超时时间(15秒)
//        grabber.setOption("stimeout", cameraDto.getNetTimeout());
//        grabber.setOption("threads", "5");
//        // grabber.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
//        // 设置缓存大小，提高画质、减少卡顿花屏
//        grabber.setOption("buffer_size", "1024000");
//
//        // 读写超时，适用于所有协议的通用读写超时
//        grabber.setOption("rw_timeout", cameraDto.getReadOrWriteTimeout());
//        // 探测视频流信息，为空默认5000000微秒
//        grabber.setOption("probesize", cameraDto.getReadOrWriteTimeout());
//        // 解析视频流信息，为空默认5000000微秒
//        grabber.setOption("analyzeduration", cameraDto.getReadOrWriteTimeout());
//
//        // 如果为rtsp流，增加配置
//        if ("rtsp".equals(cameraDto.getUrl().substring(0, 4))) {
//            // 设置打开协议tcp / udp
//            grabber.setOption("rtsp_transport", "tcp");
//            // 首选TCP进行RTP传输
//            grabber.setOption("rtsp_flags", "prefer_tcp");
//
//        }
//        try {
//            grabber.start();
//            log.info("\r\n{}\r\n启动拉流器成功", cameraDto.getUrl());
//            grabber.close();
//        } catch (FrameGrabber.Exception e) {
//            log.error("\r\n{} {}\r\n启动拉流器失败，网络超时或视频源不可用", cameraDto.getId(),cameraDto.getName());
//            grabber.close();
//            log.error(e.getMessage());
//            //			e.printStackTrace();
//        }
//
//
//        return rtsp;
//    };
    //创建点位
    public static String createMl(String token,String code,String cgId,String id,String name ) {

        String url = "http://172.20.208.40:20080/vp/cg/add_dir_item";
        Map map = new HashMap();
        map.put("cgid",cgId);
        map.put("code",code);
        map.put("id",id);
        map.put("name",name);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        JSON json = JSONUtil.parse(ruslt);
        System.out.println(ruslt);
        Mulu mulu = JSONUtil.toBean(json.getByPath("data").toString(),Mulu.class);

        return mulu.getId() ;
    }
    public static String createDw(String token,String cgId,String id,List<String>ids ) {
        String url = "http://172.20.208.40:20080/vp/cg/add_channel_items";
        Map map = new HashMap();
        map.put("cgid",cgId);
        map.put("cids",ids);
        map.put("id",id);
        String ruslt= HttpRequest.post(url).header("token",token).body(JSONUtil.toJsonStr(map))
                .execute().body();
        return null ;
    }

    public static Runnable start() throws Exception {

        return null;
    }


    public void runTake (String url){
        try {
            // Decodes-encodes
            String outputFile = "recorde.mp4";
//            frameRecord(url, outputFile,0);
        }catch (Exception e){

        }
    }
    public void releaseTake (String url){
        System.out.println("检测结束");
    }
}
